//
//  ST_BTEmptyView.swift
//  btvideo
//
//  Created by mac on 2020/3/6.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit


class ST_BTEmptyView: UIView {
    
    convenience init(title:String,imageName:UIImage) {
        
        self.init(title:title,imageName:imageName,btnTitle:"",action:nil)
        
    }
    
    
    convenience init(title:String,imageName:UIImage,btnTitle:String,action:(()->())?) {
        
        self.init(frame: CGRect.zero)
        
        icon_image_view.image = imageName
        
        content_label.text = title
        
        self.figured_height = self.label_top_constraint + 15 + 120
        
        if !btnTitle.isEmpty{
            click_btn.isHidden = false
            click_btn.setTitleForAllStates(btnTitle)
            
            self.figured_height += click_btn_top_constraint + 40
        }else{
            click_btn.isHidden = true
        }
        self.action_closure = action
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(120)
            make.top.centerX.equalToSuperview()
        }
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon_image_view.snp.bottom).offset(label_top_constraint)
        }
        
        self.addSubview(click_btn)
        click_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(33)
            make.width.equalTo(160)
            make.height.equalTo(40)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - properties
    /**Init 方法之后的高度*/
    var figured_height = 80.cgFloat
    
    fileprivate var action_closure:(()->())?
    
    fileprivate let label_top_constraint = 14.cgFloat
    
    fileprivate let click_btn_top_constraint = 33.cgFloat
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    fileprivate lazy var click_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.cornerRadius = 40/2.0
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_handle), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()

    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()

}


extension ST_BTEmptyView{
    
    @objc fileprivate func click_handle(){
        
        self.action_closure?()
    }
    
}




/**
 活跃的人 空视图 另外一个美术的 样式 囧 😶
 */
class ST_ActiveEmptyView: UIView {
    
    convenience init(title:String,imageName:UIImage) {
        
        self.init(title:title,imageName:imageName,btnTitle:"",action:nil)
        
    }
    
    
    convenience init(title:String,imageName:UIImage,btnTitle:String,action:(()->())?) {
        
        self.init(frame: CGRect.zero)
        
        icon_image_view.image = imageName
        
        content_label.text = title
        
        self.figured_height = self.label_top_constraint + 22 + 120
        
        if !btnTitle.isEmpty{
            click_btn.isHidden = false
            click_btn.setTitleForAllStates(btnTitle)
            
            self.figured_height += click_btn_top_constraint + 40
        }else{
            click_btn.isHidden = true
        }
        self.action_closure = action
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(imageSize)
            make.top.centerX.equalToSuperview()
        }
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon_image_view.snp.bottom).offset(label_top_constraint)
        }
        
        self.addSubview(click_btn)
        click_btn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(content_label.snp.bottom).offset(33)
            make.width.equalTo(160)
            make.height.equalTo(40)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//    MARK: - properties
    /**Init 方法之后的高度*/
    var figured_height = 80.cgFloat
    
    var imageSize = CGSize.init(width: 214, height: 120){
        didSet{
            self.icon_image_view.snp.updateConstraints { (make) in
                make.size.equalTo(self.imageSize)
            }
        }
    }
    
    fileprivate var action_closure:(()->())?
    
    fileprivate let label_top_constraint = 10.cgFloat
    
    fileprivate let click_btn_top_constraint = 33.cgFloat
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#ACB4C0")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    fileprivate lazy var click_btn:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.backgroundColor = Color.init(hex: 0xFEC207)
        control.cornerRadius = 40/2.0
        control.setTitleColorForAllStates(UIColor.white)
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        control.addTarget(self, action: #selector(click_handle), for: UIControl.Event.touchUpInside)
        control.isHidden = true
        return control
    }()

    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = UIView.ContentMode.scaleAspectFill
        return control
    }()

    @objc fileprivate func click_handle(){
        
        self.action_closure?()
    }
}




extension UICollectionView{
    
    func show_bitian_empty(view:ST_BTEmptyView){
        
        if self.backgroundView != nil{
            return
        }
        
        let contentView = UIView()
        
        contentView.addSubview(view)
        
        view.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-70)
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(view.figured_height)
        }
        
        self.backgroundView = contentView
    }
    
    func hidden_bitian_empty_view(){
        
        self.backgroundView = nil
    }
    
}

extension UITableView{
    
    func show_bitian_empty(view:ST_BTEmptyView){
        
        let contentView = UIView()
        
        contentView.addSubview(view)
        
        view.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.height.equalTo(view.figured_height)
        }
        
        self.backgroundView = contentView
    }
    
    func hidden_bitian_empty_view(){
        
        self.backgroundView = nil
    }
}
